package com.hnas.trade.service.impl;

import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.hnas.common.service.dto.search.DTOCommoditOrderSearch;
import com.hnas.common.service.dto.search.DTOECardOrderSearch;
import com.hnas.common.service.dto.search.DTOOrderSearch;
import com.hnas.common.util.CommonUtils;
import com.hnas.core.dto.DTOPagedList;
import com.hnas.ecard.service.dto.DTOCommoditOrder;
import com.hnas.ecard.service.dto.DTOECardOrder;
import com.hnas.ecard.service.dto.DTOSaleCardOrder;
import com.hnas.trade.dao.OrderDao;
import com.hnas.trade.dao.OrderDetailDao;
import com.hnas.trade.dao.TradeRecordDao;
import com.hnas.trade.dao.TradeRecordDetailDao;
import com.hnas.trade.orm.Order;
import com.hnas.trade.service.OrderService;
import com.hnas.trade.service.dto.DTOOrder;
import com.hnas.trade.service.dto.DTOTradeRecord;

@Component("orderService")
@Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
public class OrderServiceImpl implements OrderService{
	

	@Autowired
	private OrderDao orderDao;
	
	private static ObjectMapper objectMapper = new ObjectMapper();
	
	@Autowired
	private TradeRecordDao tradeRecordDao;
	
	@Autowired
	private OrderDetailDao orderDetailDao;
	
	@Autowired
	private TradeRecordDetailDao tradeRecordDetailDao;
	
	@Override
	public Order saveOrUpdate(DTOOrder dto) throws Exception {
		return this.orderDao.saveOrUpdate(dto);
	}

	@Override
	public DTOOrder getByOrderNo(String orderNo) {
		return this.orderDao.getByOrderNo(orderNo);
	}

	@Override
	public List<DTOOrder> getUnSuccessOrderList() {
		return this.orderDao.getUnSuccessOrderList();
	}

	@Override
	public void updateCouponStatus(String orderNo) {
		
	}

	@Override
	public List<DTOOrder> getScanMrchPayingOrderList() {
		return this.orderDao.getScanMrchPayingOrderList();
	}

	@Override
	public List<DTOOrder> getAccountOrderList() {
		return this.orderDao.getAccountOrderList();
	}

	@Override
	public Order updateOrderSaveTradeRecord(DTOOrder dto) throws Exception {
		Order order = this.orderDao.saveOrUpdate(dto);
		DTOTradeRecord tradeRecord = new DTOTradeRecord();
		BeanUtils.copyProperties(order, tradeRecord, new String[]{"id"});
		tradeRecord.setId(CommonUtils.getUUID());
		this.tradeRecordDao.saveOrUpdate(tradeRecord);
		return order;
	}

	@Override
	public DTOPagedList<DTOOrder> search(DTOOrderSearch criteria) {
		return orderDao.search(criteria);
	}

	@Override
	public List<DTOSaleCardOrder> listOrderNoPay() {
		return this.orderDao.listOrderNoPay();
	}

	@Override
	public DTOPagedList<DTOCommoditOrder> searchCommoditOrder(DTOCommoditOrderSearch criteria) {
		return this.orderDao.searchCommoditOrder(criteria);
	}

	@Override
	public DTOPagedList<DTOECardOrder> searchECardOrder(DTOECardOrderSearch criteria) {
		return orderDao.searchECardOrder(criteria);
	}

}
